﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Specialized;
using IceTea.Domain;
using IceTea.Domain.Models;

namespace IceTea.Infrastructure
{
    public class IceTeaExceptionHandleAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.ExceptionHandled == false)
            {
                IceTeaHandleErrorInfo hei = new IceTeaHandleErrorInfo(
                    filterContext.Exception,
                    filterContext.RouteData.Values["controller"].ToString(),
                    filterContext.RouteData.Values["action"].ToString(),
                    filterContext.RequestContext.HttpContext.Request.QueryString,
                    filterContext.HttpContext.Request.Form,
                    filterContext.HttpContext.User.Identity.Name);
                filterContext.Result = new ViewResult()
                {
                     ViewName = "ErrorHandlePage",
                     ViewData = new ViewDataDictionary(hei)
                };
                filterContext.ExceptionHandled = true;
            }
        }
    }

    public class IceTeaHandleErrorInfo : HandleErrorInfo
    {
        public string ErrorLogId { get; private set; }
        public string QueryString { get; private set; }
        public string FormData { get; private set; }
        public string UserName { get; private set; }

        public IceTeaHandleErrorInfo(Exception ex, string controllerName, string actionName, 
            NameValueCollection query, NameValueCollection form, string username)
            : base(ex, controllerName, actionName)
        {
            QueryString = "";
            if (query != null && query.Count > 0)
            {
                foreach (string k in query.Keys)
                {
                    QueryString = QueryString + string.Format("{0}?={1}; ", k, query[k]);
                }
            }

            FormData = "";
            if (form != null && form.Count > 0)
            {
                foreach (string k in form.Keys)
                {
                    FormData = FormData + string.Format("{0}:'{1}'; ", k, form[k]);
                }
            }
        }

        protected void SaveErrorLog()
        {
            ErrorLog log = new ErrorLog()
            {
                 ActionName = ActionName,
                 ControllerName = ControllerName,
                 ExceptionMessage = this.Exception.Message != null? this.Exception.Message : "",
                 ExceptionStackTrace = this.Exception.StackTrace != null? this.Exception.StackTrace : "",
                 FormData = this.FormData,
                 QueryString = this.QueryString,
                 UserName = this.UserName != null? this.UserName : ""
            };
            ErrorLogId = LogFactory.AddErrorLog(log).ToString();
        }
    }
}